import json
import random

from flask import Blueprint
from RootPath import RootPath
import os
from common.YmlUtil import YmlUtil
from common.contactStr import sqlContact, mqttMessageContact
from common.mqttUtil import on_mqtt_connect, publishMqttData
from common.mysqlUtil import exc_sql
generPr = Blueprint("deviceGenerator", __name__)
path = os.path.join(RootPath.getRootPath(), "Application.yml")
MQTTconf = YmlUtil().readYml(path,"mqtt_ys_22")
MySQLconf = YmlUtil().readYml(path,"mysql_ys_23")
deviceID="20060598"

class ZYSdeviceGenerator():
    def exc(self,):
        print("jinlaila =====zys=======")
        point_value={"No1FrequencyCurrent":"66","No1FrequencyOutput":json.dumps(random.randint(60,100)),
                     "No1FrequencyTemp":json.dumps(random.randint(60,100)),"BoosterPumpOutletPressure":json.dumps(random.randint(60,100)),
                     "OriginalPumpOutletPressure":json.dumps(random.randint(60,100)),"BoosterPumpInletPressure":json.dumps(random.randint(60,100)),
                     "ConcentrateWaterOutletPressure":json.dumps(random.randint(20,25)),"No1FrequencyTotalKWH":json.dumps(random.randint(60,100)),
                     "BackFlow":json.dumps(random.randint(60,100)),"No1PumprunningTime":"100",
                     "No2PumprunningTime":"100","No3PumprunningTime":"100",
                     "No4PumprunningTime":"100","No5PumprunningTime":"100",
                     "No6PumprunningTime":"100","BoosterPumpRunningTime":"100","OriginalPumpRunningTime":"100","O3PumpRunningTime":"100",
                     "WQ14":"100","BoosterPumpOutletPressureHigh":"100","BoosterPumpInletPressureLimit":"100",
                     "OriginalWaterPumpPressureHigh":"0","O3Temperature":"0"}

        statusValue={"A103Alarm":"0","A106Alarm":"0","A107Alarm":"0",
                     "A104Alarm":"0","A108Alarm":"0",
                     "A105Alarm":"0","A109Alarm":"0",
                     "A110Alarm":"0","A111Alarm":"0",
                     "A112Alarm":"0","A113Alarm":"0",
                     "A114Alarm":"0","A117Alarm":"0",
                     "MQTT_state":"1","ManualMode":"0",
                     "No1PumpRun":"0","No2PumpRun":"0",
                     "No1FrequencyOper":"0","BoosterPumpRun":"0","OriginalPumpOpen":"1",
                     "No1FrequencyOper":"0","BoosterPumpRun":"0","OriginalPumpOpen":"1",
                     "O3PumpOpen":"1","BackValveState_Yes":"1","BackValveState_No":"0","OriginalWaterState":"0","No1FrequencyFailure":"0",
                     "No1PumpFailureAlarm":"0","No2PumpFailureAlarm":"0","BoosterPumpAlarm":"0","OriginalPumpFailure":"0","OriginalPumpOverPressureAlarm":"0",
                     "BackConductivityHighAlarm":"0","BoosterPumpOverPressureAlarm":"1","NoWaterAlarm":"0"}

        point_value.update(statusValue)
        sqldata=sqlContact(deviceID,point_value)
        mqttdata=json.dumps(mqttMessageContact(deviceID,point_value))
        print(sqldata)
        print(mqttdata)
        on_mqtt_connect(MQTTconf)
        publishMqttData(MQTTconf,mqttdata)  # 推送mqtt消息要用json dump方法
        exc_sql(sqldata,MySQLconf)

# url = YmlUtil().readYml(path,"mqtt_ys_22")
# print(url)
    # ZYSdeviceGenerator().exc()
